﻿using System;
using UnityEngine;

[ExecuteInEditMode, RequireComponent(typeof(LineRenderer)), RequireComponent(typeof(BezierSpline))]
public class SplineLineRenderer : MonoBehaviour
{
    [HideInInspector, SerializeField]
    private Vector3[] allPoints = new Vector3[0];
    private LineRenderer m_lineRenderer;
    private BezierSpline m_spline;
    [Range(0.01f, 5f)]
    public float subdivision = 1f;
    public bool updateInRuntime;

    private void Update()
    {
        if ((!Application.isPlaying || this.updateInRuntime) && ((this.lineRenderer != null) && (this.spline != null)))
        {
            this.lineRenderer.useWorldSpace = false;
            this.subdivision = Mathf.Clamp(this.subdivision, 0.01f, 5f);
            this.spline.EvaluateAll(this.subdivision, ref this.allPoints);
            this.lineRenderer.SetVertexCount(this.allPoints.Length);
            this.lineRenderer.SetPositions(this.allPoints);
        }
    }

    private LineRenderer lineRenderer
    {
        get
        {
            return ((this.m_lineRenderer == null) ? (this.m_lineRenderer = base.GetComponent<LineRenderer>()) : this.m_lineRenderer);
        }
    }

    private BezierSpline spline
    {
        get
        {
            return ((this.m_spline == null) ? (this.m_spline = base.GetComponent<BezierSpline>()) : this.m_spline);
        }
    }
}

